package com.tmt.im.jpa.repository;

import com.tmt.data.jpa.repository.ITmtBaseRepo;
import com.tmt.im.jpa.entity.WalletChangeDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/5 19:59
 * @Version 1.0
 **/
@Repository
public interface WalletChangeDetailRepo extends ITmtBaseRepo<WalletChangeDetail, Long> {

    boolean existsByEventIdAndEventType(Long eventId, String eventType);

    @Query("select count(t) from WalletChangeDetail t where (:eventType is null or t.eventType = :eventType) " +
            "and (:startTime is null or t.createTime >= :startTime) " +
            "and (:endTime is null or t.createTime < :endTime)")
    Long countBy(@Param("eventType") Integer eventType,
                 @Param("startTime") Timestamp startTime,
                 @Param("endTime") Timestamp endTime);

    @Query("select t from WalletChangeDetail t where (:creatorId is null or t.creator.id = :creatorId) " +
            "and (coalesce(:eventTypes, null) is null or t.eventType in (:eventTypes)) " +
            "and (:startTime is null or t.createTime >= :startTime) " +
            "and (:endTime is null or t.createTime < :endTime) order by t.createTime")
    Page<WalletChangeDetail> queryOrderByCreateTimeAscBy(@Param("creatorId") Long creatorId,
                                                         @Param("eventTypes") List<String> eventTypes,
                                                         @Param("startTime") Timestamp startTime,
                                                         @Param("endTime") Timestamp endTime,
                                                         Pageable pageable);

    @Query("select t from WalletChangeDetail t where (:creatorId is null or t.creator.id = :creatorId) " +
            "and (coalesce(:eventTypes, null) is null or t.eventType in (:eventTypes)) " +
            "and (:startTime is null or t.createTime >= :startTime) " +
            "and (:endTime is null or t.createTime < :endTime) order by t.createTime desc")
    Page<WalletChangeDetail> queryOrderByCreateTimeDescBy(@Param("creatorId") Long creatorId,
                                                          @Param("eventTypes") List<String> eventTypes,
                                                          @Param("startTime") Timestamp startTime,
                                                          @Param("endTime") Timestamp endTime,
                                                          Pageable pageable);
}